function showPop() {
    var t = $(document).height();

    if ($('#pop-share').length <= 0) {
        $('body').append(' <div class="mod-pop" id="pop-share" onclick="hidePop(\'#pop-share\')"> <span class="text-share"></span> </div>');
    };
    var $obj=$('#pop-share');
    $($obj).height(t).fadeIn();
}

function hidePop(e) {
    $(e).fadeOut();
}


$.fn.tips=function(content){
    var tips=$(this);
    $(tips).append('<div class="ui-poptips  am-animation-fade" > <div class="ui-poptips-cnt"><i></i>'+content+'</div> </div>');
    setTimeout(function(){$("div.ui-poptips").remove();},2000);
};

//用来配置alert是否弹出
//true--可以弹出  false--不可弹出
var is_alert_message = true;
var is_defined_alert_function = true; //是否使用自定义的alert弹出方式  

//重写alert，使其可以配置
var old_alert = window.alert;
window.alert = function(msg){
	if(is_alert_message == true){
		if(is_defined_alert_function == true){
			$("body").tips(msg);
		} else {
			old_alert(msg);			
		}
	} else {
		return;
	}
};



var App = function(){
	var COOKIE_TOKEN = "ZS_ZS_CT";
	var COOKIE_TOKEN_ENCRYPT = 'ZS_ZS__ENCRTPT';
	var COOKIE_TOKEN_CITY = 'ZS_ZS__CITY';
	var COOKIE_TOKEN_CITY_GET = 'ZS_ZS__CITY_IS_GET';
	var COOKIE_TOKEN_CITY_USER_USE = 'ZS_ZS__CITY_USE';
	var COOKIE_TOKEN_ZIXUN_ZS_READ = 'ZS_ZS__ZIXUN_ZS_READ';
	
	var COOKIE_IMG_URL = 'ZS_ZS_IMG_URL';
	
	// IE mode
    var isIE8 = false;
    var isIE9 = false;
    var isIE10 = false;
    var cookieCity = {};
    var useCookieCity = {};
    var cookieData = {};
    var pageSize = "20"; //分页大小
    var projectName = "zhisheng-pc";
    var loginUrl = "/" + projectName + "/views/login/login.html";
    var personUrl = "/" + projectName + "/views/user/myRace.html";
    var modifyPasswordPageUrl = "/" + projectName + "/views/user/modifyPassword.html";
    var zhucePageUrl = "/" + projectName + "/views/login/zhuce.html";
    var cookieExpire = 15;
    
    
    var handleInit = function(){
		isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
	    isIE9 = !! navigator.userAgent.match(/MSIE 9.0/);
	    isIE10 = !! navigator.userAgent.match(/MSIE 10.0/);
	    pageSize = "20";
    };
    
    
	var isLogin = function(){
		var rmb = $.cookie(COOKIE_TOKEN);
		if(typeof(rmb) === "undefined" || rmb == null || rmb == "" || rmb == "null"){
			cookieData = {};
			return false;
		}
		cookieData = JSON.parse(rmb);
		var user_id = cookieData["id"];
		if( typeof(user_id) === "undefined" || user_id == null || user_id == ""){
			return false;
		}
		return true;
	};
	
	//=========定位得到的城市
	var _setCity = function(cityObj){
		if(cityObj == null || cityObj.org_code == null || cityObj.org_code == ""
			|| cityObj.org_code == "[]"){
			return;
		}
		var json = JSON.stringify(cityObj);
		$.removeCookie(COOKIE_TOKEN_CITY, { path : "/"});
		$.cookie(COOKIE_TOKEN_CITY, json, { path : "/"});
	};
	
	var _getCity = function(){
		var rmb = $.cookie(COOKIE_TOKEN_CITY);
		if(typeof(rmb) === "undefined" || rmb == null || rmb == "" || rmb == "null"){
			cookieCity = {};
			return false;
		}
		cookieCity = JSON.parse(rmb);
		return cookieCity;
	};
	
	var _setGetCity = function(){
		$.removeCookie(COOKIE_TOKEN_CITY_GET, { path : "/"});
		$.cookie(COOKIE_TOKEN_CITY_GET, "IS", { path : "/"});
	}; 
	
	var _getGetCity = function(){
		var rmb = $.cookie(COOKIE_TOKEN_CITY_GET);
		if(typeof(rmb) === "undefined" || rmb == null || rmb == "" || rmb == "null"){
			return false;
		}
		if(rmb == "IS"){
			return true;
		}
		return false;
	};

	
	//+++++++++++++++
	//{"org_code" : , "org_name" :  }
	var _setUseCity = function(cityObj){
		if(cityObj == null || cityObj.org_code == null || cityObj.org_code == ""
			|| cityObj.org_code == "[]"){
			return;
		}
		
		var json = JSON.stringify(cityObj);
		$.removeCookie(COOKIE_TOKEN_CITY_USER_USE, { path : "/"});
		$.cookie(COOKIE_TOKEN_CITY_USER_USE, json, { path : "/"});
	};
	
	
	var _getUseCity = function(){
		var rmb = $.cookie(COOKIE_TOKEN_CITY_USER_USE);
		if(typeof(rmb) === "undefined" || rmb == null || rmb == "" || rmb == "null"){
			useCookieCity = {};
			return false;
		}
		useCookieCity = JSON.parse(rmb);
		return useCookieCity;
	};
	
	var _setReadNews = function(id){
		if(id > 0){
			var zsStr = $.cookie(COOKIE_TOKEN_ZIXUN_ZS_READ);
			if(typeof(zsStr) === "undefined" || zsStr == null || zsStr == "" || zsStr == "null"){
				$.removeCookie(COOKIE_TOKEN_ZIXUN_ZS_READ, { path : "/"});
				$.cookie(COOKIE_TOKEN_ZIXUN_ZS_READ, id, { path : "/"});
			} else {
				var array = zsStr.split(",");
				if($.inArray(id, array) == -1){
					array.push(id);
					var jtsr = array.toString();
					$.removeCookie(COOKIE_TOKEN_ZIXUN_ZS_READ, { path : "/"});
					$.cookie(COOKIE_TOKEN_ZIXUN_ZS_READ, jtsr, { path : "/"});
				}
			}
		}
	};
		
	var _getReadNews = function(id){
		if(id > 0){
			var zsStr = $.cookie(COOKIE_TOKEN_ZIXUN_ZS_READ);
			if(typeof(zsStr) !== "undefined" && zsStr != null && zsStr != "" && zsStr != "null"){
				var array = zsStr.split(",");
				var temp = id.toString();
				if($.inArray(temp, array) != -1){
					return true;
				}
			}
		}
		return false;
	};
	
	
	
	
	/**
	 * userInfo
	 */
	var _setUser = function(userInfo){
		var cookieData1 = {};
		cookieData1.id=userInfo.id;
		cookieData1.sex = userInfo.sex;
		cookieData1.nickname = userInfo.nickname;
		cookieData1.mobile = userInfo.mobile;
		cookieData1.name = userInfo.name;
		cookieData1.zs_bean = userInfo.zs_bean;
		cookieData1.user_icon = userInfo.user_icon;
		cookieData1.zs_diamond = userInfo.zs_diamond;
		cookieData1.token = userInfo.token;
		console.log(cookieData1);
		var json = JSON.stringify(cookieData1);
		console.log(json);
		$.removeCookie(COOKIE_TOKEN_ENCRYPT, { path : "/"});
		$.cookie(COOKIE_TOKEN_ENCRYPT, userInfo.token, { path : "/"});
		$.removeCookie(COOKIE_TOKEN, { path : "/"});
		$.cookie(COOKIE_TOKEN, json, { path : "/" , expires : cookieExpire});
	};
	
	
	var _loginOutRemoveCookie = function(){
		$.removeCookie(COOKIE_TOKEN_ENCRYPT, { path : "/"});
		$.removeCookie(COOKIE_TOKEN, { path : "/"});
		$.removeCookie(COOKIE_TOKEN_CITY, { path : "/"});
		$.removeCookie(COOKIE_TOKEN_CITY_GET, { path : "/"});
		$.removeCookie(COOKIE_TOKEN_CITY_USER_USE, { path : "/"});
	};
	
	
	var _changeCookieNameSex = function(nickName, username, sex){
		cookieData.nickname = nickName;
		cookieData.name = username;
		cookieData.sex = sex;
		var json = JSON.stringify(cookieData);
		$.removeCookie(COOKIE_TOKEN, { path : "/"});
		$.cookie(COOKIE_TOKEN, json, { path : "/" , expires : cookieExpire});
	};
	
	var _changeCookieHeadImg = function(img){
		cookieData.user_icon = img;
		var json = JSON.stringify(cookieData);
		$.removeCookie(COOKIE_TOKEN, { path : "/"});
		$.cookie(COOKIE_TOKEN, json, { path : "/" , expires : cookieExpire});
	};
	
	var _changeCookiePhone = function(phone){
		cookieData.mobile = phone;
		var json = JSON.stringify(cookieData);
		$.removeCookie(COOKIE_TOKEN, { path : "/"});
		$.cookie(COOKIE_TOKEN, json, { path : "/" , expires : cookieExpire});
	};
	
	
	var _setImgPreCookie = function(pre){
		$.removeCookie(COOKIE_IMG_URL, { path : "/"});
		$.cookie(COOKIE_IMG_URL, pre, { path : "/" });
	};
	
	var _getImgPreCookie = function(pre){
		var rmb = $.cookie(COOKIE_IMG_URL);
		if(typeof(rmb) === "undefined" || rmb === undefined || rmb == null || rmb == "" || rmb == "null"){
			return false;
		}
		return rmb;
	};
	
	
	/** 
	 * 对日期进行格式化， 
	 * @param date 要格式化的日期   时间戳
	 * @param format 进行格式化的模式字符串
	 *     支持的模式字母有： 
	 *     y:年, 
	 *     M:年中的月份(1-12), 
	 *     d:月份中的天(1-31), 
	 *     h:小时(0-23), 
	 *     m:分(0-59), 
	 *     s:秒(0-59), 
	 *     S:毫秒(0-999),
	 *     q:季度(1-4)
	 * @return String
	 * @author 
	 */
	var dateFormat = function (date, format) {
	    if (typeof date === "string") {
	        var mts = date.match(/(\/Date\((\d+)\)\/)/);
	        if (mts && mts.length >= 3) {
	            date = parseInt(mts[2]);
	        }
	    }
	    date = new Date(date);
	    if (!date || date.toUTCString() == "Invalid Date") {
	        return "";
	    }
	    var map = {
	        "M": date.getMonth() + 1, //月份 
	        "d": date.getDate(), //日 
	        "h": date.getHours(), //小时 
	        "m": date.getMinutes(), //分 
	        "s": date.getSeconds(), //秒 
	        "q": Math.floor((date.getMonth() + 3) / 3), //季度 
	        "S": date.getMilliseconds() //毫秒 
	    };
	    
	    format = format.replace(/([yMdhmsqS])+/g, function(all, t){
	        var v = map[t];
	        if(v !== undefined){
	            if(all.length > 1){
	                v = '0' + v;
	                v = v.substr(v.length-2);
	            }
	            return v;
	        }
	        else if(t === 'y'){
	            return (date.getFullYear() + '').substr(4 - all.length);
	        }
	        return all;
	    });
	    return format;
	};
	
	//yyyy-MM-dd时间的比较
	var yyyymmddDuibi = function(begin, end){
		var arr = begin.split("-");
	    var starttime = new Date(arr[0], arr[1], arr[2]);
	    var starttimes = starttime.getTime();
	    var arrs = end.split("-");
	    var endtime = new Date(arrs[0], arrs[1], arrs[2]);
	    var endtimes = endtime.getTime();
	    if (starttimes > endtimes) {
	        return false;
	    }
	    return true;
	};
	
	
	//yyyy-MM-dd HH:ii时间的比较
	var yyyymmddhhiissDuibi = function(begin, end){
		var startDatetimeArray = begin.split(" ");
		var startArr = startDatetimeArray[0].split("-");
		var startTimes = startDatetimeArray[1].split(":");
		var startMonth = parseInt(startArr[1]);
		startMonth = startMonth - 1;
	    var starttime = new Date(parseInt(startArr[0]), startMonth, parseInt(startArr[2]), parseInt(startTimes[0]), parseInt(startTimes[1]), parseInt(startTimes[2]));
	    var starttimes = starttime.getTime();
	    
	    var endDatetimeArray = end.split(" ");
	    var endArrs = endDatetimeArray[0].split("-");
	    var endTimes = endDatetimeArray[1].split(":");
	    var endMonth = parseInt(endArrs[1]);
	    endMonth = endMonth - 1;
	    var endtime = new Date(parseInt(endArrs[0]), endMonth, parseInt(endArrs[2]), parseInt(endTimes[0]), parseInt(endTimes[1]), parseInt(endTimes[2]));
	    var endtimes = endtime.getTime();
	    if (starttimes > endtimes) {
	        return false;
	    }
	    return true;
	};
	
	/**
	 * 和当前时间比较
	 */
	var _yyyymmddhhiissDuibiNow = function(begin){
		var startDatetimeArray = begin.split(" ");
		var startArr = startDatetimeArray[0].split("-");
		var startTimes = startDatetimeArray[1].split(":");
		var startMonth = parseInt(startArr[1]);
		startMonth = startMonth - 1;
	    var starttime = new Date(parseInt(startArr[0]), startMonth, parseInt(startArr[2]), parseInt(startTimes[0]), parseInt(startTimes[1]), parseInt(startTimes[2]));
	    var starttimes = starttime.getTime();

	    var now = new Date().getTime();
	    if (starttimes > now) {
	        return false;
	    }
	    return true;
	};
	//yyyy-mm-dd hh:ii:ss
	var _formatDatetime = function(datetime){
		var startDatetimeArray = datetime.split(" ");
		var startArr = startDatetimeArray[0].split("-");
		var startTimes = startDatetimeArray[1].split(":");
		var startMonth = parseInt(startArr[1]);
		startMonth = startMonth - 1;
	    var starttime = new Date(parseInt(startArr[0]), startMonth, parseInt(startArr[2]), parseInt(startTimes[0]), parseInt(startTimes[1]), parseInt(startTimes[2]));
	    var currentDate = new Date(),
	    	current = currentDate.getTime(),
	    	year = currentDate.getFullYear(),
	    	time = current - starttime;
        if(time < 120000){
            return "刚刚";
        }else if(time < 3600000){
            return Math.floor(time/60000)  + "分钟前";
        }else if (time < 86400000) {
            return Math.floor(time / 3600000) + "小时前";
        }else if(time < 172800000){
        	return "昨天 "+ startTimes[0] + ":" + startTimes[1];
        }else if (time < 259200000){
        	return "前天 "+ startTimes[0] + ":" + startTimes[1];
        } else if ((year + "") == startArr[0]){
        	return startArr[1] + "-" + startArr[2] + " " + startTimes[0] + ":" + startTimes[1];
        } else {
        	return startArr[0] + "-" + startArr[1] + "-" + startArr[2];
        }
	    
	};
	
	
	
	var _trim = function(v){
		if (!v) return v;
        return v.replace(/^\s+/g, '').replace(/\s+$/g, '');
	};

	
	var subContent = function(content, sLength){
		if(content === undefined || content == null || content == ""){
			return "";
		}
		var length = content.length;
		if(length >= sLength){
			return content.substr(0, sLength) + "……";
		}
		return content;
	};
	
	
	
	
	
	return {
		changeCookiePhone : function(phone){
			_changeCookiePhone(phone);
		},
		changeCookieHeadImg : function(img){
			_changeCookieHeadImg(img);
		},
		changeCookieNameSex : function(nickName, username, sex){
			_changeCookieNameSex(nickName, username, sex);
		},
		loginOutRemoveCookie : function(){
			_loginOutRemoveCookie();
		},
		setUser : function(userInfo){
			_setUser(userInfo);
		},
		getCookieExpire :function(){
			return cookieExpire;//保存天
		},
		getCookieTokenEncrypt : function(){
			return COOKIE_TOKEN_ENCRYPT;
		},
		getCookieToken : function(){
			return COOKIE_TOKEN;
		},
		//初始化
		init : function(){
			handleInit();
		},
		
		// check IE8 mode
        isIE8: function () {
            return isIE8;
        },

        // check IE9 mode
        isIE9: function () {
            return isIE9;
        },
        
        isIE10 : function(){
        	return isIE10;
        },
        
        checkLogin : function(){
        	return isLogin();
        },
        
        loginUrl : function(){
        	return loginUrl;
        },
        getPersonUrl : function(){
        	return personUrl;
        },
        dateFormat : function(date, format){
        	return dateFormat(date, format);
        },
        
        isPassword : function(value){
        	//只能是6-12个字母或数字
        	return (/[0-9a-zA-Z]{6,12}/).test(_trim(value));
        },
        
        isEmpty : function(value){
    		return !!_trim(value);
        },
        
        _trim : function(value) {
        	return _trim(value);
        },
        
        isNumber : function(value) {
        	value = _trim(value);
        	return (/^\d+(.\d*)?$/).test(value);
        },
        getCookeData : function(){
        	return cookieData;
        },
        
        getPageSize : function(){
        	return pageSize;
        },
        yyyymmddDuibi : function (begin, end) {
        	return yyyymmddDuibi(begin, end);
        },
        subContent : function(content, sLength){
        	if(sLength == undefined || sLength == null || sLength == ""){
        		sLength = 15;
        	}
        	return subContent(content, sLength);
        },
        yyyymmddhhiissDuibiNow : function (date){
        	return _yyyymmddhhiissDuibiNow(date);
        },
        setCity : function(cityJsonObj){
        	_setCity(cityJsonObj);
        },
        setGetCity : function(){
        	_setGetCity();
        },
        getGetCity : function(){
        	return _getGetCity();
        },
        getCity : function(){
        	return _getCity();
        },
        setUseCity : function(cityJsonObj){
        	_setUseCity(cityJsonObj);
        },
        getUseCity : function(){
        	return _getUseCity();
        },
        getModifyPasswordPageUrl : function(){
        	return modifyPasswordPageUrl;
        },
        getZhucePageUrl : function(){
        	return zhucePageUrl;
        },
        setImgPreCookie : function(pre){
        	_setImgPreCookie(pre);
        },
        getImgPreCookie : function(){
        	return _getImgPreCookie();
        },
        formateNewDateTime : function(datetime){
        	return _formatDatetime(datetime);
        },
        setReadNews : function(id){
        	_setReadNews(id);
        },
        getReadNews : function(id){
        	return _getReadNews(id);
        }
        
	};
}();



/**
 ** 加法函数，用来得到精确的加法结果
 ** 说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
 ** 调用：accAdd(arg1,arg2)
 ** 返回值：arg1加上arg2的精确结果
 **/
function accAdd(arg1, arg2) {
    var r1, r2, m, c;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2));
    if (c > 0) {
        var cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", "")) * cm;
        } else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm;
            arg2 = Number(arg2.toString().replace(".", ""));
        }
    } else {
        arg1 = Number(arg1.toString().replace(".", ""));
        arg2 = Number(arg2.toString().replace(".", ""));
    }
    return (arg1 + arg2) / m;
}




//给Number类型增加一个add方法，调用起来更加方便。
Number.prototype.add = function (arg) {
    return accAdd(arg, this);
};
		  
/**
 ** 减法函数，用来得到精确的减法结果
 ** 说明：javascript的减法结果会有误差，在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。
 ** 调用：accSub(arg1,arg2)
 ** 返回值：arg1加上arg2的精确结果
 **/
function accSub(arg1, arg2) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.sub = function (arg) {
    return accSub(this, arg);
};

/**
 ** 乘法函数，用来得到精确的乘法结果
 ** 说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
 ** 调用：accMul(arg1,arg2)
 ** 返回值：arg1乘以 arg2的精确结果
 **/
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    }
    catch (e) {
    }
    var num=Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    return num.toFixed(2);
}

// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.mul = function (arg) {
    return accMul(arg, this);
};


/***拼接url
**/
function parse_forward_url(targetLocation, paramObject, removeEmpty){
	var type = $.type( targetLocation ),
         factorIndex = -1,
         name,
         paramStr = "";
    if ( type !== "string" ) {
        throw new TypeError( "targetLocation 必须是string类型，实际上是 " + type );
    }
    removeEmpty = !!removeEmpty;
    factorIndex = targetLocation.indexOf( "?" );
    type = $.type( paramObject );
    if ( type === "object" ) {
        for( name in paramObject ) {
            if ( paramObject.hasOwnProperty( name ) ) {
                if ( paramObject[name] == null ) {
                    if ( !removeEmpty ) {
                        paramStr += "&" + name + "=";
                    }
                } else {
                    paramStr += "&" + name + "=" + paramObject[name];
                }
            }
        }
    } else {
        paramStr = "&" + paramObject;
    }
    if ( paramStr !== "" ) {
        if ( factorIndex === -1 ) {
            targetLocation = targetLocation + "?" + paramStr.substring( 1 );
        } else if ( factorIndex === targetLocation.length - 1 ) {
            targetLocation = targetLocation + paramStr.substring( 1 );
        } else {
            targetLocation = targetLocation + paramStr;
        }
    }
    return encodeURI( targetLocation );
}


